from typing import Optional

from ninja import FilterSchema, ModelSchema, Field, Schema
from django.db.models import Q
from rest_framework import serializers

from models.models import CustomerServiceRecords, CustomerServiceFeedbackRecord, CommonQuestion, KnowledgeBase, \
    CustomerManage
from water_applets import settings


class CustomerServiceRecordFiltersSchema(FilterSchema):
    """用户筛选"""
    user: Optional[str] = None
    question_time: Optional[dict] = None
    answer_time: Optional[dict] = None

    def filter_user(self, value) -> Q:
        return Q(openid__icontains=value) | Q(nickname__icontains=value) | Q(phone_number__icontains=value)

    def filter_question_time(self, value: dict):
        if value is None:
            return Q()
        if value.get('start') and not value.get('end'):
            return Q(question_time__gt=value.get('start'))
        if not value.get('start') and value.get('end'):
            return Q(question_time__lt=value.get('end'))
        if value.get('start') and value.get('end'):
            return Q(question_time__gt=value.get('start'), question_time__lt=value.get('end'))
        return Q()

    def filter_answer_time(self, value: dict):
        if value is None:
            return Q()
        if value.get('start') and not value.get('end'):
            return Q(answer_time__gt=value.get('start'))
        if not value.get('start') and value.get('end'):
            return Q(answer_time__lt=value.get('end'))
        if value.get('start') and value.get('end'):
            return Q(answer_time__gt=value.get('start'), answer_time__lt=value.get('end'))
        return Q()

    class Config:
        fields = "__all__"


class CustomerServiceRecordResponseSchema(ModelSchema):
    class Config:
        model = CustomerServiceRecords
        model_fields = "__all__"


class CustomerServiceFeedbackRecordFiltersSchema(FilterSchema):
    """用户筛选"""
    score: Optional[dict] = None
    create_time: Optional[dict] = None

    def filter_score(self, value: dict):
        if value is None:
            return Q()
        if value.get('start') and not value.get('end'):
            return Q(score__gt=value.get('start'))
        if not value.get('start') and value.get('end'):
            return Q(score__lt=value.get('end'))
        if value.get('start') and value.get('end'):
            return Q(score__gt=value.get('start'), score__lt=value.get('end'))
        return Q()

    def filter_create_time(self, value: dict):
        if value is None:
            return Q()
        if value.get('start') and not value.get('end'):
            return Q(create_time__gt=value.get('start'))
        if not value.get('start') and value.get('end'):
            return Q(create_time__lt=value.get('end'))
        if value.get('start') and value.get('end'):
            return Q(create_time__gt=value.get('start'), create_time__lt=value.get('end'))
        return Q()

    class Config:
        fields = "__all__"


class CustomerServiceFeedbackRecordResponseSchema(ModelSchema):
    class Config:
        model = CustomerServiceFeedbackRecord
        model_fields = "__all__"


class CommonQuestionResponseSchema(ModelSchema):
    class Config:
        model = CommonQuestion
        model_fields = "__all__"


class CustomerManageResponseSchema(ModelSchema):

    class Config:
        model = CustomerManage
        model_fields = "__all__"


class KnowledgeBaseResponseSchema(ModelSchema):
    class Config:
        model = KnowledgeBase
        model_fields = "__all__"


class KnowledgeBasSerializer(serializers.ModelSerializer):
    file = serializers.SerializerMethodField

    def get_file(self, obj):
        header = "http://"
        if settings.WEB_SERVER_IS_HTTPS:
            header = "https://"
        return f'{header}{settings.WEB_SERVER_IPV4}:{settings.WEB_SERVER_PORT}/{obj.file}'

    class Meta:
        model = KnowledgeBase
        fields = "__all__"


class AddCommonQuestionSchema(Schema):
    question: str = Field(default="", description="问题内容")
    answer: str = Field(default="", description="回答内容")


class AddKnowledgeBaseSchema(Schema):
    file: str = Field(default="", description="文件")


class KnowledgeBaseSerializer(serializers.ModelSerializer):
    class Meta:
        model = KnowledgeBase
        fields = '__all__'


class CustomerManageSerializer(serializers.ModelSerializer):

    class Meta:
        model = CustomerManage
        fields = '__all__'


class AddServiceSchema(Schema):
    customer_number: str = Field(default="", description="客服编号")
    status: int = Field(default=1, description="状态")


class ChangeServiceStatusSchema(Schema):
    status: str = Field(default="", description="状态")
